package com.wondertek.web.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wondertek.service.entity.business.DeviceErrorRecordPo;
import com.wondertek.service.entity.business.DevicePo;
import com.wondertek.service.sdk.iotdh.dto.res.PushDhMessage;
import com.wondertek.service.service.business.DeviceErrorRecordService;
import com.wondertek.service.service.business.DeviceService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @description: 设备未报警定时任务
 * @author: qinjianhui
 * @create: 2024-09-06 15:02
 **/
//@Component
public class DeviceNotAlarmTask {

    @Resource
    private DeviceErrorRecordService deviceErrorRecordService;

    @Resource
    private DeviceService deviceService;

    @Scheduled(cron = "0 0 12 * * ?")
    public void start() {
        //先去查询设置了未报警的网关
        LambdaQueryWrapper<DevicePo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DevicePo::getIsDelete,0);
        lambdaQueryWrapper.eq(DevicePo::getDeviceTypeId,"37");
        lambdaQueryWrapper.apply("not_alarm_day is not null");
        lambdaQueryWrapper.apply("not_alarm_day >0");
        List<DevicePo> devicePoList = deviceService.list(lambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(devicePoList)){
            for(DevicePo devicePo : devicePoList){
                LambdaQueryWrapper<DeviceErrorRecordPo> deviceErrorRecordPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                deviceErrorRecordPoLambdaQueryWrapper.eq(DeviceErrorRecordPo::getDeviceId,devicePo.getSnCode());
                deviceErrorRecordPoLambdaQueryWrapper.apply("error_type != 'Alarm hub not responding for a long time'");
                deviceErrorRecordPoLambdaQueryWrapper.orderByDesc(DeviceErrorRecordPo::getErrorTime);
                List<DeviceErrorRecordPo> list = deviceErrorRecordService.list(deviceErrorRecordPoLambdaQueryWrapper);
                if(CollectionUtils.isNotEmpty(list)){
                    DeviceErrorRecordPo deviceErrorRecord = list.get(0);
                    Date errorTime = deviceErrorRecord.getErrorTime();
                    LocalDateTime localDate = errorTime.toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
                    localDate = localDate.plusDays(devicePo.getNotAlarmDay());
                    //现在-上次上报时间>设置时间--发送
                    if(localDate.isBefore(LocalDateTime.now())) {
                        this.send(devicePo.getSnCode(), devicePo.getDeviceName());
                    }
                }else {
                    //新增后未发送过警报的
                    //现在-创建>设置时间--发送
                    Date createTime = devicePo.getCreateTime();
                    LocalDateTime localDate = createTime.toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
                    localDate = localDate.plusDays(devicePo.getNotAlarmDay());
                    if(localDate.isBefore(LocalDateTime.now())) {
                        this.send(devicePo.getSnCode(), devicePo.getDeviceName());
                    }
                }
            }
        }
    }

    private void send(String snCode, String deviceName){
        PushDhMessage pushDhMessage = new PushDhMessage();
        pushDhMessage.setType(2);
        pushDhMessage.setDeviceSn(snCode);
        pushDhMessage.setName(deviceName);
        pushDhMessage.setMsgType("Alarm hub not responding for a long time");
        pushDhMessage.setAlarmTime(System.currentTimeMillis());
        deviceErrorRecordService.rollBackErrorRecord(pushDhMessage);
    }
}

